package com.hxh.generater.springBoot.simple.gen;

import com.hxh.entity.Project;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.simple.SimpleConfig;
import com.hxh.generater.springBoot.genBase.GenTableCRUD;
import com.hxh.generater.springBoot.simple.gen.crud.*;
import com.hxh.generater.springBoot.util.GetAllTables;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.springBoot.genBase
 * @ClassName: GenTableCRUD
 * @Author: HXH
 * @Description: 生成表的crud
 * @Date: 2024/5/21 17:00
 */
@Slf4j
public class GenSimpleTableCRUD implements GenTableCRUD {

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成实体类
     * @date 2024/5/21
     **/
    @Override
    public void generationEntity(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenEntity.generationOneEntity(project, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Entity异常！", e);
            throw e;
        }
    }

    @Override
    public String generationEntity(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenEntity.generationEntityClass(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Entity异常！", e);
            throw e;
        }
    }

    @Override
    public String generationEntityInput(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenEntity.generationEntityInputClass(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成EntityInput异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成mybatis的mapper.xml
     * @date 2024/5/21
     **/
    @Override
    public void generationMapper(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenMapper.generationMapper(project, classInfo);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public String generationMapper(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenMapper.generationMapper(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Mapper异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成dao层接口类
     * @date 2024/5/21
     **/
    @Override
    public void generationDaoClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenDao.generationDaoClass(project, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Dao异常！", e);
            throw e;
        }
    }

    @Override
    public String generationDaoClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenDao.generationDaoClass(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Dao异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成service层代码
     * @date 2024/5/21
     **/
    @Override
    public void generationServiceClass(Project project, ClassInfo classInfo) throws Exception {

    }

    @Override
    public String generationServiceClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        return "";
    }

    @Override
    public void generationServiceImplClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenService.generationServiceClass(project, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Service异常！", e);
            throw e;
        }
    }

    @Override
    public String generationServiceImplClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenService.generationServiceClass(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Service异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成controller层代码
     * @date 2024/5/21
     **/
    @Override
    public void generationControllerClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenController.generationControllerClass(project, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Controller异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName    : 包名
     * @param classInfo      :
     * @param templateConfig
     * @return void
     * @author HXH
     * @description:
     * @date 2024/5/23
     **/
    @Override
    public String generationControllerClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenController.generationControllerClass(packageName, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Controller异常！", e);
            throw e;
        }
    }


    /**
     * @param project         :
     * @param initExtendTable : 忽略的表名集合
     * @return void
     * @author HXH
     * @description: 是否生成自定义表的crud 是：生成数据库中所有表的crud   否：只生成模板框架的crud
     * @date 2024/5/21
     **/
    @Override
    public void generationAllTableCRUD(Project project, boolean initExtendTable) throws Exception {
        try {
            List<ClassInfo> classInfos;
            if (initExtendTable) {
                classInfos = GetAllTables.generationAllTableInfo(project);
            } else {
                classInfos = GetAllTables.generationAllTableInfoByList(project, SimpleConfig.simpleTemplateTableList);
            }
            for (ClassInfo classInfo : classInfos) {
                generationEntity(project, classInfo);
                generationMapper(project, classInfo);
                generationDaoClass(project, classInfo);
                generationServiceImplClass(project, classInfo);
                generationControllerClass(project, classInfo);
            }
        } catch (Exception e) {
            log.error("simple模板：生成全部表的crud异常！", e);
            throw e;
        }
    }

    /**
     * @param project
     * @param classInfo
     * @throws Exception
     */
    @Override
    public void generationEntityMappingClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenEntity.generationOneEntityMappingClass(project, classInfo);
        } catch (Exception e) {
            log.error("simple模板：生成Entity异常！", e);
            throw e;
        }
    }
}
